<script lang="ts" setup>
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue'
import { queryCustomerRequestData, queryCustomerTableCol } from '@/api/customer/types/customer'
import { queryCustomer, CustomerRemove } from '@/api/customer'
import { useTable } from '@/hooks/useTable'
import { ElMessage, FormInstance } from 'element-plus'
import { getToken } from '@/utils/cache/cookies'
import { formatterRole } from '@/utils/formatter'
defineOptions({
  name: 'CustomerList'
})
const form = reactive<queryCustomerRequestData>({
  name: '',
  phone: '',
  role: ''
})
const formRef = ref<FormInstance | null>(null)
const { data, loading, pagination, load, handleCurrentChange, handleSizeChange, setCurrentPage } =
  useTable<queryCustomerTableCol>(queryCustomer, getToken() ?? '', () => form)
const handleFormSearch = () => {
  setCurrentPage(1)

  load()
}
const reset = () => {
  formRef.value?.resetFields()
}
onMounted(() => {
  load()
})

const delCustomer = (row: queryCustomerTableCol) => {
  CustomerRemove({
    token: getToken() ?? '',
    phone: row.phone
  }).then((res) => {
    if (res.statusCode === '0') {
      ElMessage.success(res.data)
      load()
    }
  })
}
const updateCustomer = (row: queryCustomerTableCol) => {
  ChangeCustomerDialogRef.value?.setData(row.name, row.phone, row.role, row.id)
  ChangeCustomerDialogRef.value?.open()
}
const ChangeCustomerDialog = defineAsyncComponent(
  () => import('@/components/dialog/ChangeCustomer.vue')
)
const ChangeCustomerDialogRef = ref<InstanceType<typeof ChangeCustomerDialog> | null>(null)

const open = () => {
  ChangeCustomerDialogRef.value?.open()
}
</script>

<template>
  <div class="xy-container">
    <el-form :model="form" ref="formRef" inline>
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="name" label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="role" label="角色">
        <el-select v-model="form.role" placeholder="请输入" style="width: 120px">
          <el-option label="不限" value="" />
          <el-option label="客服" value="customer" />
          <el-option label="管理员" value="admin" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFormSearch">查询</el-button>
        <el-button @click="reset" plain>重置</el-button>
      </el-form-item>
      <el-form-item float-right>
        <el-button type="primary" @click="open">添加客服</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data" v-loading="loading" class="no-border">
      <el-table-column prop="name" label="姓名" align="center" />
      <el-table-column prop="phone" label="手机号" align="center" />
      <el-table-column prop="role" label="角色" align="center" :formatter="formatterRole" />
      <el-table-column prop="openId" label="微信识别号" show-overflow-tooltip align="center" />
      <el-table-column prop="createTime" label="注册时间" align="center" />
      <el-table-column prop="lastLoginTime" label="上次登录时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" class="is-show" @click="delCustomer(scope.row)"
            >删除</el-button
          >
          <el-button link type="primary" class="is-show" @click="updateCustomer(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
    <change-customer-dialog ref="ChangeCustomerDialogRef" @refresh="load" />
  </div>
</template>

<style scoped></style>
